package com.usefullc.blog.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.system.validator.UsefulValid;
import com.usefullc.blog.domain.Category;
import com.usefullc.blog.param.CategoryParam;
import com.usefullc.blog.vo.CategoryVo;
import com.usefullc.blog.query.CategoryQuery;
import com.usefullc.blog.service.CategoryService;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.system.controller.BaseController;
import com.usefullc.system.controller.WebResultBuilder;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.param.IdsParam;
import com.usefullc.system.vo.TreeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import java.util.List;

import static com.usefullc.system.controller.WebResultBuilder.successResult;

/**
 * 类目控制类
 * @author: 星空
 * @date: 20220628
 */
@RestController
@RequestMapping("/blog/category/")
@Api( tags="类目")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "类目列表",response = WebResult.class)
    @GetMapping(path = "list",produces = "application/json")
    public WebResult list(@UsefulValid(query = true) CategoryQuery query) {
        Pager<CategoryVo> page = categoryService.findVoByPage(query);
        return WebResultBuilder.successResult(page);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "获取所有列表", response = WebResult.class)
    @GetMapping(path = "listAll", produces = "application/json")
    public WebResult listAll(@UsefulValid(query = true) CategoryQuery query) {
        List<Category> dataList = categoryService.findAll(query);
        return WebResultBuilder.successResult(dataList);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "获取树列表", response = WebResult.class)
    @GetMapping(path = "listTree", produces = "application/json")
    public WebResult listTree(CategoryQuery query) {
        List<TreeVo> dataList = categoryService.listTree(query);
        return WebResultBuilder.successResult(dataList);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "类目详情",response = WebResult.class)
    @GetMapping(path = "detail",produces = "application/json")
    public WebResult detail(@ApiParam(value = "示例ID") @Valid @RequestParam Long id) {
        Category category = categoryService.findById(id);
        return WebResultBuilder.successResult(category);
    }


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "创建类目", response = WebResult.class)
    @PostMapping(path = "/save", produces = "application/json")
    @UsefulValid
    public WebResult save(@UsefulValid(save=true) @RequestBody CategoryParam param) {
        Category category = BeanUtils.beanCopy(param, Category.class);
        categoryService.saveCategory(category);
        return WebResultBuilder.successResult(category);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "修改类目", response = WebResult.class)
    @PostMapping(path = "/update", produces = "application/json")
    public WebResult update(@UsefulValid(update=true) @RequestBody CategoryParam param) {
        Category category = BeanUtils.beanCopy(param, Category.class);
        categoryService.updateCategory(category);
        return successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "del",produces = "application/json")
    @ApiOperation(value = "删除类目",response = WebResult.class)
    public WebResult del(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        categoryService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @PostMapping(path = "delByLogic",produces = "application/json")
    @ApiOperation(value = "逻辑删除类目",response = WebResult.class)
    public WebResult delByLogic(@UsefulValid(del=true) @RequestBody IdsParam idParam) {
        categoryService.delete(idParam.getIds());
        return WebResultBuilder.successResult();
    }







}
